package com.example.leetdemo.process;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution998 {

    public TreeNode insertIntoMaxTree(TreeNode root, int val) {
        if (val > root.val) {
            return new TreeNode(val, root, null);
        }
        List<TreeNode> list = new ArrayList<>();
        list.add(root);
        while (list.size() > 0) {
            List<TreeNode> temp = new ArrayList<>();
            for (TreeNode treeNode : list) {
//                if (treeNode.left != null && treeNode.left.val < val) {
//                    TreeNode treeNode1 = new TreeNode(val, treeNode.left.left, treeNode.left);
//                    treeNode.left.left = null;
//                    treeNode.left = treeNode1;
//                    temp.clear();
//                    break;
//                } else if (treeNode.left != null) {
//                    temp.add(treeNode.left);
//                }
                if (treeNode.right != null && treeNode.right.val < val) {
                    TreeNode treeNode1 = new TreeNode(val, null, treeNode.right);
                    treeNode.right.right = null;
                    treeNode.right = treeNode1;
                    temp.clear();
                    break;
                } else if (treeNode.right != null) {
                    temp.add(treeNode.right);
                }
            }

            list = temp;
        }
        Map<Integer, Integer> map = new HashMap<>();
        return root;
    }


    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
